﻿using Microsoft.Extensions.DependencyInjection;
using Modules.System.Attachment;
using PmSoft;

namespace Modules.Corp
{
    [UseDI(ServiceLifetime.Scoped)]
    public class CorpAttachmentService
    {
        private readonly CorpAttachmentRepository repository;
        private readonly AttachmentService attachmentService;

        public CorpAttachmentService(CorpAttachmentRepository repository, AttachmentService attachmentService)
        {
            this.repository = repository;
            this.attachmentService = attachmentService;
        }

        public void Create(CorpAttachment entity)
        {
            if (string.IsNullOrEmpty(entity.AttachmentId))
                throw new ArgumentNullException(nameof(entity.AttachmentId));
            repository.Insert(entity);
            ToggleTemporaryAttachment(new List<CorpAttachment> { entity });
        }

        public void Deleted(IEnumerable<string> ids)
        {
            foreach (string id in ids)
                DeleteAttachment(id);
        }

        /// <summary>
        /// 通过objectId获取附件集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<AttachmentInfo> GetSysAttachmentByObjectIdsAndType(IEnumerable<object> objedctIds, 企业附件类型 attachmentType, 企业附件租户 tenant)
        {
            var data = repository.GetAttachmentsByObjectId(objedctIds).Where(a => a.AttachmentType == attachmentType && a.Tenant == tenant);
            return attachmentService.GetAttachmentInfos(data.Select(a => a.AttachmentId));
        }

        /// <summary>
        /// 通过objectId获取附件集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<AttachmentInfo> GetSysAttachmentByObjectIdAndType(object objedctId, 企业附件类型 attachmentType, 企业附件租户 tenant)
        {
            var data = repository.GetAttachmentsByObjectId(objedctId).Where(a => a.AttachmentType == attachmentType && a.Tenant == tenant);
            return attachmentService.GetAttachmentInfos(data.Select(a => a.AttachmentId));
        }

        /// <summary>
        /// 通过objectId获取附件集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<AttachmentInfo> GetSysAttachmentByObjectIdAndType(object objedctId, 企业附件租户 tenant)
        {
            var data = repository.GetAttachmentsByObjectId(objedctId).Where(a => a.Tenant == tenant);
            return attachmentService.GetAttachmentInfos(data.Select(a => a.AttachmentId));
        }

        /// <summary>
        /// 通过objectId获取附件集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CorpAttachment> GetCorpAttachmentsByObjectIdAndType(object objedctId, 企业附件类型 attachmentType, 企业附件租户 tenant)
        {
            var data = repository.GetAttachmentsByObjectId(objedctId).Where(a => a.AttachmentType == attachmentType && a.Tenant == tenant);
            return data;
        }

        public IEnumerable<CorpAttachment> GetCorpAttachmentsByObjectIdAndType(object objedctId, 企业附件租户 tenant)
        {
            var data = repository.GetAttachmentsByObjectId(objedctId).Where(a => a.Tenant == tenant);
            return data;
        }

        /// <summary>
        /// 通过objectId获取附件集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CorpAttachment> GetCorpAttachmentByObjectIdsAndType(IEnumerable<object> objedctIds, 企业附件类型 attachmentType, 企业附件租户 tenant)
        {
            var data = repository.GetAttachmentsByObjectIds(objedctIds).Where(a => a.AttachmentType == attachmentType && a.Tenant == tenant);
            return data;
        }

        public void DeleteAttachment(string id)
        {
            repository.DeleteByEntityId(id);
        }

        /// <summary>
        /// 上传临时附件到MinIO
        /// </summary>
        /// <param name="notifyAttachments"></param>
        public void ToggleTemporaryAttachment(IEnumerable<CorpAttachment> notifyAttachments)
        {
            attachmentService.ToggleTemporaryAttachments(notifyAttachments);
        }
    }
}